﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShareMarking.Service;
using ShareMarking.Entity;
using ShareMarking.Interface;
using System.Data;
using MySoft.Data;

namespace ShareMarking.Check
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckNC();
            CheckHospital();
            CheckPNR();
            //CheckStore();
        }

        private static void CheckNC()
        {
            ComLib.LogLib.Log4NetBase.Log("---------------开始检查NC！");
            try
            {
                DbSession.Default.FromSql("DELETE qr_check where qrtype=1").Execute();

                DataTable table = new DataTable();
                string sql = @"SELECT nc.nc_code,qr.scene_id,qr.qrcode_url,qr.code_url
from nc_ncuser nc left join qr_QrCodeRelate rel on nc.nc_code=rel.rel_Code 
left join qr_QrCode qr on rel.qr_Scene_ID=qr.scene_id where rel.IsDelete=0";

                table = DbSession.Default.FromSql(sql).ToTable();
                table.TableName = "checktb";

                CheckQrcode check = new CheckQrcode();
                Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var imgurl = table.Rows[i]["qrcode_url"].ToString();
                    var qrcodecontent = check.getqrcontent(imgurl);
                    var resultwx = vigo.GetWXContent("", table.Rows[i]["scene_id"].ToString());

                    var vigomessage1 = "title:" + resultwx.message[0].title
                            + "desc:" + resultwx.message[0].shortdesc
                            + "pic:" + resultwx.message[0].picurl
                            + "link:" + resultwx.message[0].linkurl;
                    var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                            + "desc:一键注册，终身礼遇"
                            + "pic:http://admin.mjn.howjoin.com/images/p1.jpg"
                            + "link:" + "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=188$NCCode=" 
                                + table.Rows[i]["nc_code"].ToString() + "$openId=####";
                    var vigomessage2 = "title:" + resultwx.message[1].title
                            + "desc:" + resultwx.message[1].shortdesc
                            + "pic:" + resultwx.message[1].picurl
                            + "link:" + resultwx.message[1].linkurl;
                    var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                            + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                            + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                    var vigomessage3 = "title:" + resultwx.message[2].title
                            + "desc:" + resultwx.message[2].shortdesc
                            + "pic:" + resultwx.message[2].picurl
                            + "link:" + resultwx.message[2].linkurl;
                    var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                            + "desc:惊喜连连，准备好了么？"
                            + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";
                    
                    qr_Check qrc = new qr_Check()
                    {
                        qrtype = "1",
                        rel_code = table.Rows[i]["nc_code"].ToString(),
                        scene_id = table.Rows[i]["scene_id"].ToString(),

                        code_url = table.Rows[i]["code_url"].ToString(),
                        httpcode_url = qrcodecontent,
                        iseqccode_url = (table.Rows[i]["code_url"].ToString() == qrcodecontent),

                        message1 = message1,
                        vigomessage1 = vigomessage1,
                        iseq1 = (message1 == vigomessage1),

                        message2 = message2,
                        vigomessage2 = vigomessage2,
                        iseq2 = (message2 == vigomessage2),

                        message3 = message3,
                        vigomessage3 = vigomessage3,
                        iseq3 = (message3 == vigomessage3)
                    };
                    qr_CheckService.Instance.Add_Entity(qrc);
                }
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("检查NC错误:" + ex.Message);
            }
        }

        private static void CheckHospital()
        {
            ComLib.LogLib.Log4NetBase.Log("---------------开始检查Hospital！");
            try
            {
                DbSession.Default.FromSql("DELETE qr_check where qrtype=2").Execute();

                DataTable table = new DataTable();
                string sql = @"SELECT hp.code__c,hp.msr_staffid__c,hp.Active__c,qr.scene_id,qr.qrcode_url,qr.code_url,qr.wx_title,qr.wx_content,qr.wx_imgurl,qr.wx_url
from sfdc_hospital hp left join qr_QrCodeRelate rel on hp.code__c=rel.rel_Code 
left join qr_QrCode qr on rel.qr_Scene_ID=qr.scene_id where rel.IsDelete=0";

                table = DbSession.Default.FromSql(sql).ToTable();
                table.TableName = "checktb";

                CheckQrcode check = new CheckQrcode();
                Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var imgurl = table.Rows[i]["qrcode_url"].ToString();
                    var qrcodecontent = check.getqrcontent(imgurl);
                    var resultwx = vigo.GetWXContent("", table.Rows[i]["scene_id"].ToString());

                    string picandlink;
                    if (table.Rows[i]["Active__c"].ToString().ConvertTo<bool>() == true)
                    {
                        picandlink = "pic:http://admin.mjn.howjoin.com/images/p1.jpg"
                            + "link:" + string.Format("http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=174$NCCode={0}$PNRCode={1}$openId=####",
                            table.Rows[i]["code__c"].ToString(), table.Rows[i]["msr_staffid__c"].ToString());
                    }
                    else
                    {
                        picandlink = "pic:http://admin.mjn.howjoin.com/images/p1.jpg"
                            + "link:http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=174$openId=####";
                    }

                    var vigomessage1 = "title:" + resultwx.message[0].title
                            + "desc:" + resultwx.message[0].shortdesc
                            + "pic:" + resultwx.message[0].picurl
                            + "link:" + resultwx.message[0].linkurl;
                    var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                            + "desc:一键注册，终身礼遇" + picandlink;
                            
                    var vigomessage2 = "title:" + resultwx.message[1].title
                            + "desc:" + resultwx.message[1].shortdesc
                            + "pic:" + resultwx.message[1].picurl
                            + "link:" + resultwx.message[1].linkurl;
                    var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                            + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                            + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                    var vigomessage3 = "title:" + resultwx.message[2].title
                            + "desc:" + resultwx.message[2].shortdesc
                            + "pic:" + resultwx.message[2].picurl
                            + "link:" + resultwx.message[2].linkurl;
                    var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                            + "desc:惊喜连连，准备好了么？"
                            + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";
                    

                    qr_Check qrc = new qr_Check()
                    {
                        qrtype = "2",
                        rel_code = table.Rows[i]["code__c"].ToString(),
                        scene_id = table.Rows[i]["scene_id"].ToString(),

                        code_url = table.Rows[i]["code_url"].ToString(),
                        httpcode_url = qrcodecontent,
                        iseqccode_url = (table.Rows[i]["code_url"].ToString() == qrcodecontent),

                        message1 = message1,
                        vigomessage1 = vigomessage1,
                        iseq1 = (message1 == vigomessage1),

                        message2 = message2,
                        vigomessage2 = vigomessage2,
                        iseq2 = (message2 == vigomessage2),

                        message3 = message3,
                        vigomessage3 = vigomessage3,
                        iseq3 = (message3 == vigomessage3)

                    };
                    qr_CheckService.Instance.Add_Entity(qrc);
                }
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("检查HOSP错误:" + ex.Message);
            }
        }

        private static void CheckPNR()
        {
            ComLib.LogLib.Log4NetBase.Log("---------------开始检查PNR！");
            try
            {
                DbSession.Default.FromSql("DELETE qr_check where qrtype=3").Execute();

                DataTable table = new DataTable();
                string sql = @"SELECT pnr.employeenumber__c,pnr.IsActive__c,qr.scene_id,qr.qrcode_url,qr.code_url,qr.wx_title,qr.wx_content,qr.wx_imgurl,qr.wx_url
from sfdc_pnr pnr left join qr_QrCodeRelate rel on pnr.employeenumber__c=rel.rel_Code 
left join qr_QrCode qr on rel.qr_Scene_ID=qr.scene_id where rel.IsDelete=0";

                table = DbSession.Default.FromSql(sql).ToTable();
                table.TableName = "checktb";

                CheckQrcode check = new CheckQrcode();
                Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var imgurl = table.Rows[i]["qrcode_url"].ToString();
                    var qrcodecontent = check.getqrcontent(imgurl);
                    var resultwx = vigo.GetWXContent("", table.Rows[i]["scene_id"].ToString());

                    string link;
                    if (table.Rows[i]["IsActive__c"].ToString().ConvertTo<bool>() == true)
                    {
                        link = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?regSubChannel=174$PNRCode="
                            + table.Rows[i]["employeenumber__c"].ToString() + "$openId=####";
                    }
                    else
                    {
                        link = "http://crmweb.meadjohnson.com.cn/mjnwx/client_phone.action?openId=####";
                    }

                    var vigomessage1 = "title:" + resultwx.message[0].title
                            + "desc:" + resultwx.message[0].shortdesc
                            + "pic:" + resultwx.message[0].picurl
                            + "link:" + resultwx.message[0].linkurl;
                    var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                            + "desc:一键注册，终身礼遇"
                            + "pic:http://admin.mjn.howjoin.com/images/p1.jpg"
                            + "link:" + link;
                    var vigomessage2 = "title:" + resultwx.message[1].title
                            + "desc:" + resultwx.message[1].shortdesc
                            + "pic:" + resultwx.message[1].picurl
                            + "link:" + resultwx.message[1].linkurl;
                    var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                            + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                            + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                    var vigomessage3 = "title:" + resultwx.message[2].title
                            + "desc:" + resultwx.message[2].shortdesc
                            + "pic:" + resultwx.message[2].picurl
                            + "link:" + resultwx.message[2].linkurl;
                    var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                            + "desc:惊喜连连，准备好了么？"
                            + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";
                    

                    qr_Check qrc = new qr_Check()
                    {
                        qrtype = "3",
                        rel_code = table.Rows[i]["employeenumber__c"].ToString(),
                        scene_id = table.Rows[i]["scene_id"].ToString(),

                        code_url = table.Rows[i]["code_url"].ToString(),
                        httpcode_url = qrcodecontent,
                        iseqccode_url = (table.Rows[i]["code_url"].ToString() == qrcodecontent),

                        message1 = message1,
                        vigomessage1 = vigomessage1,
                        iseq1 = (message1 == vigomessage1),

                        message2 = message2,
                        vigomessage2 = vigomessage2,
                        iseq2 = (message2 == vigomessage2),

                        message3 = message3,
                        vigomessage3 = vigomessage3,
                        iseq3 = (message3 == vigomessage3)
                    };
                    qr_CheckService.Instance.Add_Entity(qrc);
                }
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("检查PNR错误:" + ex.Message);
            }
        }

        private static void CheckStore()
        {
            ComLib.LogLib.Log4NetBase.Log("---------------开始检查Store！");
            try
            {
                DbSession.Default.FromSql("DELETE qr_check where qrtype=1002").Execute();

                DataTable table = new DataTable();
                string sql = @"SELECT st.storecode,qr.scene_id,qr.qrcode_url,qr.code_url,qr.wx_title,qr.wx_content,qr.wx_imgurl,qr.wx_url
from qr_Store st left join qr_QrCodeRelate rel on st.storecode=rel.rel_Code 
left join qr_QrCode qr on rel.qr_Scene_ID=qr.scene_id where rel.IsDelete=0 AND st.createon>CONVERT(datetime,'2016-05-12 00:00:00')";

                table = DbSession.Default.FromSql(sql).ToTable();
                table.TableName = "checktb";

                CheckQrcode check = new CheckQrcode();
                Qrcode_vigo vigo = new Qrcode_vigo("http://my.erpcoo.com/index.php/Api/Qrcode/index", "sdf5sf5et5e42t5");

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var imgurl = table.Rows[i]["qrcode_url"].ToString();
                    var qrcodecontent = check.getqrcontent(imgurl);
                    var resultwx = vigo.GetWXContent("", table.Rows[i]["scene_id"].ToString());


                    var vigomessage1 = "title:" + resultwx.message[0].title
                            + "desc:" + resultwx.message[0].shortdesc
                            + "pic:" + resultwx.message[0].picurl
                            + "link:" + resultwx.message[0].linkurl;
                    var message1 = "title:马上加入美赞臣A＋妈妈会，尊享360°孕育礼遇"
                            + "desc:一键注册，终身礼遇"
                            + "pic:http://admin.mjn.howjoin.com/images/p1.jpg"
                            + "link:" + "http://wx.ismartgo.com/app/lighthouse/index.aspx?from=" 
                            + table.Rows[i]["storecode"].ToString() + "$type=2$openid=####";
                    var vigomessage2 = "title:" + resultwx.message[1].title
                            + "desc:" + resultwx.message[1].shortdesc
                            + "pic:" + resultwx.message[1].picurl
                            + "link:" + resultwx.message[1].linkurl;
                    var message2 = "title:不可错过的海量专业孕育课程＆胎教早教资源！"
                            + "desc:有声资源每周更新，满足妈妈＆宝宝共同需求"
                            + "pic:http://admin.mjn.howjoin.com/images/p2.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=2$sn=0d2328393e16964b17e11e30bb85378f$scene=1$srcid=06209qW7BXxURv3bBLvzMGHZ";
                    var vigomessage3 = "title:" + resultwx.message[2].title
                            + "desc:" + resultwx.message[2].shortdesc
                            + "pic:" + resultwx.message[2].picurl
                            + "link:" + resultwx.message[2].linkurl;
                    var message3 = "title:【会员福利】奶粉、京东优惠券、巧虎礼包、滴滴红包等你拿,还有……"
                            + "desc:惊喜连连，准备好了么？"
                            + "pic:http://admin.mjn.howjoin.com/images/p3.jpg"
                            + "link:" + "http://mp.weixin.qq.com/s?__biz=MjM5MjM1NjMzNA==$mid=2656105052$idx=3$sn=8478a978359403d68c1452f132455d32$scene=1$srcid=0620AZL0kIEOrjKTGX6Y2emW";
                    
                    qr_Check qrc = new qr_Check()
                    {
                        qrtype = "1002",
                        rel_code = table.Rows[i]["storecode"].ToString(),
                        scene_id = table.Rows[i]["scene_id"].ToString(),

                        code_url = table.Rows[i]["code_url"].ToString(),
                        httpcode_url = qrcodecontent,
                        iseqccode_url = (table.Rows[i]["code_url"].ToString() == qrcodecontent),

                        message1 = message1,
                        vigomessage1 = vigomessage1,
                        iseq1 = (message1 == vigomessage1),

                        message2 = message2,
                        vigomessage2 = vigomessage2,
                        iseq2 = (message2 == vigomessage2),

                        message3 = message3,
                        vigomessage3 = vigomessage3,
                        iseq3 = (message3 == vigomessage3)

                    };
                    qr_CheckService.Instance.Add_Entity(qrc);
                }
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("检查PNR错误:" + ex.Message);
            }
        }
    }
}
